
exclude_dirs := ./srv ./api ./web


# dirs := $(shell find ./srv -maxdepth 1 -type d)
# find out the folder that has Makefile
# dirs := $(shell find ./srv -type d -exec test -e '{}'/Makefile \;  -print)

dirs := $(shell find ${exclude_dirs} -name Makefile -print0 | xargs -0 -n1 dirname | sort --unique)
SUBDIRS := $(filter-out $(exclude_dirs),$(dirs))

GOOS=linux
GOARCH=amd64


$(info dirs: ${dirs})
$(info SUBDIRS: ${SUBDIRS})

$(info platform: ${GOOS})
$(info arch: ${GOARCH})
$(info GOPATH: ${GOPATH})

.PHONY: help
help: 
	@echo '${SUBDIRS}'
	@echo 'Management commands for shippy:'
	@echo
	@echo 'Usage:'
	@echo '    make build           Compile the project.'
	@echo '    make mod-tidy        runs go mod tidy, mostly used for ci.'
	@echo '    make build-alpine    Compile optimized for alpine linux.'
	@echo '    make package         Build final docker image with just the go binary inside'
	@echo '    make tag             Tag image created by package with latest, git commit and version'
	@echo '    make test            Run tests on a compiled project.'
# @echo '    make push            Push tagged images to registry'
	@echo '    make clean           Clean the directory tree.'
	@echo '    make proto           Generate *.proto.go from proto definition.'
	@echo


TOPTARGETS := build clean test mod-tidy proto build-alpine package tag 

$(TOPTARGETS): 
	@echo " ######### $@ ### MAKECMDGOALS: $(MAKECMDGOALS) "
	@for app in ${SUBDIRS}; do \
		echo "starting to $(MAKECMDGOALS) $$app"; \
		make -C $$app $(MAKECMDGOALS) ; \
		echo "end of $(MAKECMDGOALS) $$app\n";	\
	done 

default: help

.PHONY: 
	$(TOPTARGETS) help default







